package com.pucheng.bracelet.ui.fragment.sleep

import android.os.Bundle
import android.os.Handler
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.util.DatetimeUtil
import com.pucheng.bracelet.ui.view.SleepProgressBar
import com.pucheng.bracelet.viewmodel.sleep.SleepViewModel
import com.pucheng.bracelet.viewmodel.steps.StepsToDayViewModel
import kotlinx.android.synthetic.main.fragment_sleep_to_day.*
import me.hgj.jetpackmvvm.base.fragment.BaseVmFragment


class SleepToDayFragment : BaseVmFragment<StepsToDayViewModel>() {

    private val sleepViewModel: SleepViewModel by activityViewModels()

    override fun createObserver() {
        sleepViewModel.apply {
//            sleepData.observe(viewLifecycleOwner, Observer {
//                if (it != null) {
//                    tv_date.text = DatetimeUtil.formatDate(it.timestamp,DatetimeUtil.DATE_PATTERN_EE)
//                    tv_sleep_time_hour.text = DatetimeUtil.minToHourAndMin(it.totalTime)
//                }
//            })
            sleepTo7Day.observe(viewLifecycleOwner, Observer {
                if (!it.isNullOrEmpty()) {
                    tv_date.text = DatetimeUtil.formatDate(it.last().timestamp,DatetimeUtil.DATE_PATTERN_EE)
                    val timeStr = DatetimeUtil.minToHourAndMin(it.last().totalTime)
                    val spanText = SpannableString(timeStr)
                    val hourLen = timeStr.indexOf(getString(R.string.hour))
                    spanText.setSpan(
                        AbsoluteSizeSpan(35, true), 0, hourLen,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                    )
                    spanText.setSpan(
                        AbsoluteSizeSpan(20, true), hourLen, hourLen+getString(R.string.hour).length,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                    )
                    spanText.setSpan(
                        AbsoluteSizeSpan(35, true), hourLen+getString(R.string.hour).length, timeStr.indexOf(getString(R.string.minute)),
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                    )
                    spanText.setSpan(
                        AbsoluteSizeSpan(20, true), timeStr.indexOf(getString(R.string.minute)), timeStr.length,
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                    )
                    tv_sleep_time_hour.text = spanText
                    sleep_info.setSleepInfo(it.last())

//                    for (node in it.last().nodes) {
////                        pb_sleep.setf
//                        if (node.status != 0) {
//                            Handler().postDelayed({
////                                pb_sleep =
////                                when (node.status) {
////                                    1 ->
////                                        resources.getColor(R.color.color_light)
////                                    2 ->
////                                        resources.getColor(R.color.color_restful)
////                                    3 ->
////                                        resources.getColor(R.color.color_rem)
////                                    else ->
////                                        resources.getColor(android.R.color.transparent)
////                                }
//                                pb_sleep.setProgress(node.min_offset)
//                            },1000)
//                        }
//
//                    }
                }
            })
        }
    }

    override fun dismissLoading() {
    }

    override fun initView(savedInstanceState: Bundle?) {

//        val arrow =
//            resources.getDrawable(R.mipmap.sleep)
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            if (arrow != null) {
//                arrow.isAutoMirrored = true
//            }
//        }
//        img_sleep.setImageDrawable(arrow)
    }

    override fun layoutId(): Int = R.layout.fragment_sleep_to_day

    override fun lazyLoadData() {

    }

    override fun showLoading(message: String) {
    }

    companion object {
        fun newInstance(): SleepToDayFragment {
            val fragment = SleepToDayFragment()
            return fragment
        }
    }
}